<template>
  <div
    class="fixed top-0 left-0 w-screen h-screen text-xl z-20 backdrop-blur-4xl bg-transparent pb-2 overflow-y-auto xl:p-2"
  >
    <z-navbar v-if="isMobileTerminal" sticky>
      {{ pexelsData.title }}
      <template #right>
        <z-svg-icon
          name="share"
          class="w-3 h-3"
          fillClass="fill-zinc-900 dark:fill-zinc-200"
          @click="onShareClick"
        ></z-svg-icon>
      </template>
    </z-navbar>
    <!-- pc -->
    <z-svg-icon
      v-else
      name="close"
      class="w-3 h-3 ml-1 p-0.5 cursor-pointer duration-200 rounded-sm hover:bg-zinc-100 absolute top-2 right-2"
      fillClass="fill-zinc-400"
      @click="onPop"
    ></z-svg-icon>

    <!-- 内容区域 -->
    <div
      v-if="pexelsData.title"
      class="xl:w-[80%] xl:h-full xl:mx-auto xl:rounded-lg xl:flex"
    >
      <img
        class="w-screen mb-2 xl:w-3/5 xl:h-full xl:rounded-tl-lg xl:rounded-bl-lg"
        :src="pexelsData.photo"
        alt=""
      />
      <div
        class="xl:w-2/5 xl:h-full xl:bg-white xl:dark:bg-zinc-900 xl:rounded-tr-lg xl:rounded-br-lg xl:p-3"
      >
        <div v-if="!isMobileTerminal" class="flex justify-between mb-2">
          <!-- 分享 -->
          <z-svg-icon
            name="share"
            class="w-4 h-4 p-1 cursor-pointer hover:bg-zinc-200 dark:hover:bg-zinc-800 duration-300 rounded"
            fillClass="fill-zinc-900 dark:fill-zinc-200"
            @click="onShareClick"
          ></z-svg-icon>
          <!-- 收藏 -->
          <z-button
            type="info"
            icon="heart"
            iconClass="fill-zinc-900 dark:fill-zinc-200"
          ></z-button>
        </div>

        <!-- 标题、作者 -->
        <p
          class="text-base text-zinc-900 dark:text-zinc-200 ml-1 font-bold xl:text-xl xl:mb-5"
        >
          {{ pexelsData.title }}
        </p>
        <div class="flex items-center mt-1 px-1">
          <img
            v-lazy
            class="h-3 w-3 rounded-full"
            :src="pexelsData.avatar"
            alt=""
          />
          <span class="text-base text-zinc-900 dark:text-zinc-200 ml-1">{{
            pexelsData.author
          }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { getPexelsFromId } from '@/api/pexels'
import { isMobileTerminal } from '@/utils/flexible'
import { useRouter } from 'vue-router'
import { useAppStore } from '@/store/modules/app'
import { weiboShare } from '@/utils/share'

const router = useRouter()
const appStore = useAppStore()
const props = defineProps({
  id: {
    type: String,
    required: true
  }
})

const pexelsData = ref({})
const getPexelsData = async () => {
  const data = await getPexelsFromId(props.id)
  pexelsData.value = data
}
getPexelsData()

/**
 * 关闭详情页
 */
const onPop = () => {
  appStore.changeRouterType('back')
  router.back()
}

/**
 * 分享按钮点击处理
 */
const onShareClick = () => {
  weiboShare(
    pexelsData.value.photo,
    `${window.location.origin}/pins/${pexelsData.value.id}`
  )
}
</script>

<style lang="scss" scoped></style>
